﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class RoleACL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("UB2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["office"] == null || Request.QueryString["role"] == null)
                {
                    lblError.Text = "Bad page calling!";
                }
                else
                {
                    lblOffice.Text = Request.QueryString["office"];
                    lblRole.Text = Request.QueryString["role"];
                }
            }
            //btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            //btnRemove.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
            lstAvailFunc2.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UB3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_role_add_acl");
                SqlParameter para = cmd.Parameters.AddWithValue("@office", lblOffice.Text);
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@role", lblRole.Text);
                para = cmd.Parameters.AddWithValue("@access_code", lstAvailFunc2.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@user_office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                    lstAvailFunc2.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "office not found!";
                else if (ret_code == 2)
                    lblError.Text = "Adding failed!";
                else if (ret_code == 3)
                    lblError.Text = "func_id not found!";
                else if (ret_code == 4)
                    lblError.Text = "Authority needed!";
                else
                    lblError.Text = "Unknow Error(" + ret_code.ToString() + ")";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UB3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the function you want to delete.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_role_delete_acl";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", lblOffice.Text);
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@role", lblRole.Text);
                para = cmd.Parameters.AddWithValue("@access_code", key);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@user_office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                    lstAvailFunc2.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Wrong airline";
                else if (ret_code == 2)
                    lblError.Text = "delete failed";
                else if (ret_code == 4)
                    lblError.Text = "authority needed";
                else
                    lblError.Text = "Unknow Error(" + ret_code.ToString() + ")";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
    }
}